/*******************************************************************************
 * Copyright (c) 2010 liw.
 * All rights reserved. 
 * 
 * This file is part of VanBus.
 * 
 *     VanBus is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     VanBus is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with VanBus.  If not, see <http://www.gnu.org/licenses/>.
 * Contributors:
 *     liw - initial API and implementation
 ******************************************************************************/
package org.niclab.vanbus.activity.comp;

import java.util.HashMap;

import org.niclab.vanbus.R;
import org.niclab.vanbus.activity.controller.ReminderController;
import org.niclab.vanbus.application.VanBusApplication;
import org.niclab.vanbus.model.BusRoute;
import org.niclab.vanbus.model.BusStop;
import org.niclab.vanbus.model.EndStopReminder;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteConstraintException;
import android.location.LocationManager;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class EndStopReminderDialog {
	
	private static final String LOG_TAG="EndStopReminderDialog"; 

	private Activity activity;
	private Context context;
	
	private ReminderController reminderController;
	private EndStopReminder reminderModel;


	private String titleMsg;
	private String textMsg="Set a reminder fot this stop as an end stop of your trip?";
	private String gpsWarningMsg = "Warning: Your GPS satellites service is currently disabled. This reminder requires GPS service.";
	
	private boolean warning = false;
	private View layout;
	private TextView msgView;
	private TextView warningMsgView;
	
	public EndStopReminderDialog(Activity parentActivity, EndStopReminder reminderModel){
		this.activity = parentActivity;
		this.context = parentActivity.getApplicationContext();
		reminderController = new ReminderController(context);
		this.reminderModel = reminderModel;
		
		layout=activity.getLayoutInflater().inflate(R.layout.reminder_end_stop_dialog, null);
		
		msgView = (TextView) layout.findViewById(R.id.reminder_end_stop_dialog_message);
	
		warningMsgView = (TextView) layout.findViewById(R.id.reminder_end_stop_dialog_gps_warning);
		
	}
	
	public EndStopReminderDialog(Activity parentActivity){
		this(parentActivity,null);
		
	}
	
	public AlertDialog createDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle("Set a reminder");
		builder.setView(layout);
		msgView.setText(textMsg);
		warningMsgView.setText(gpsWarningMsg);
	
		builder.setPositiveButton("Set a Reminder",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				try{
					setUpReminder();
					}catch(SQLiteConstraintException e){
					AlertDialog.Builder builder = new AlertDialog.Builder(activity);
					builder.setMessage("Sorry, there is already an end stop reminder for this stop.");
					
					builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
						
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							
						}
					});
					
					builder.create().show();
				}
			}

				
		
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();					
			}
		});
		
		
		
		AlertDialog dialog =builder.create();
		return dialog;
					
	}
	
	public void prepareDialog(Dialog dialog){
		AlertDialog adialog = (AlertDialog) dialog;
		Log.d(LOG_TAG,"title:"+titleMsg);
		adialog.setTitle(titleMsg);
		
		if (!VanBusApplication.getInstance().getMyLocationManager().isGPSEnabled()) {
			warningMsgView.setVisibility(View.VISIBLE);

		}else
			warningMsgView.setVisibility(View.GONE);
		
			
		
	}
	
	public void setTitle(String msg){
		this.titleMsg = msg;
	}


	public void setReminder(EndStopReminder reminder){
		this.reminderModel = reminder;
		
	}
	
	public void setUpReminder(){
		
		reminderController.setEndStopReminder(reminderModel);
		
		
	}
	
	


}
